package com.example.chengww.myone.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.chengww.myone.R;
import com.example.chengww.myone.adapter.MyListAdapter;
import com.example.chengww.myone.bean.Constants;
import com.example.chengww.myone.bean.ReadADContent;
import com.example.chengww.myone.utils.HTTPUtils;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.util.List;

public class ReadADContentActivity extends AppCompatActivity {

    private int id;
    private RelativeLayout read_ad_layout,read_ad_top,read_ad_bottom;
    private TextView tv_title,tv_bottom;
    private ImageView imageView3,back;
    private ListView listView;
    private List<ReadADContent.Data> datas;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 100){
                MyListAdapter listAdapter = new MyListAdapter(datas,
                        ReadADContentActivity.this);
                listView.setAdapter(listAdapter);
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        new Thread(new Runnable() {
            @Override
            public void run() {
                String json = HTTPUtils.getStringByInputStream(HTTPUtils.getInputStream(
                                Constants.READING.READING_AD_PATH+id));
                Gson gson = new Gson();
                ReadADContent adContent = gson.fromJson(json,ReadADContent.class);
                datas = adContent.getData();
                handler.obtainMessage(100).sendToTarget();
            }
        }).start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_adcontent);

        read_ad_layout = (RelativeLayout) findViewById(R.id.read_ad_layout);
        read_ad_top = (RelativeLayout) findViewById(R.id.read_ad_top);
        read_ad_bottom = (RelativeLayout) LayoutInflater.from(this).
                inflate(R.layout.read_ad_bottom,null);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_bottom = (TextView) read_ad_bottom.findViewById(R.id.tv_bottom);
        imageView3 = (ImageView) read_ad_bottom.findViewById(R.id.imageView3);
        listView = (ListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ReadADContentActivity.this,
                        ReadEssayActivity.class);
                intent.putExtra("type",datas.get(position).getType()-1);
                intent.putExtra("id",datas.get(position).getItem_id());
                intent.putExtra("radio",false);
                startActivity(intent);
            }
        });
        listView.addFooterView(read_ad_bottom);

        back = (ImageView) findViewById(R.id.img_close);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.activity_in,R.anim.activity_out);
            }
        });
        Intent intent = ReadADContentActivity.this.getIntent();
        id = intent.getIntExtra("id",0);
        tv_title.setText(intent.getStringExtra("title"));
        read_ad_top.setBackgroundColor(
                Color.parseColor(intent.getStringExtra("bgcolor")));
        tv_bottom.setText(intent.getStringExtra("bottom_text"));
        Picasso.with(this).load(intent.getStringExtra("cover")).into(imageView3);
        read_ad_layout.setBackgroundColor(
                Color.parseColor(intent.getStringExtra("bgcolor")));


    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.activity_in,R.anim.activity_out);
    }
}
